<template>
    <div class="p-2">
        <el-row :gutter="20">
            <el-col :lg="4" :xs="24">
                <el-card shadow="hover">
                    <el-input v-model="deptName" placeholder="请输入部门名称" prefix-icon="Search" clearable />
                    <el-tree ref="deptTreeRef" class="mt-2" node-key="id" :data="deptOptions"
                        :props="{ label: 'label', children: 'children' }" :expand-on-click-node="false"
                        :filter-node-method="filterNode" highlight-current default-expand-all
                        @node-click="handleNodeClick" />
                </el-card>
            </el-col>
            <el-col :lg="20" :xs="24">
                <transition :enter-active-class="animate.searchAnimate.enter"
                    :leave-active-class="animate.searchAnimate.leave">
                    <div v-show="showSearch" class="mb-[10px]">
                        <el-card shadow="hover">
                            <el-form ref="queryFormRef" :model="queryParams" :inline="true">
                                <el-form-item label="岗位编码" prop="postCode">
                                    <el-input v-model="queryParams.postCode" placeholder="请输入岗位编码" clearable
                                        @keyup.enter="handleQuery" />
                                </el-form-item>
                                <el-form-item label="类别编码" prop="postCategory">
                                    <el-input v-model="queryParams.postCategory" placeholder="请输入类别编码" clearable
                                        style="width: 200px" @keyup.enter="handleQuery" />
                                </el-form-item>
                                <el-form-item label="岗位名称" prop="postName">
                                    <el-input v-model="queryParams.postName" placeholder="请输入岗位名称" clearable
                                        @keyup.enter="handleQuery" />
                                </el-form-item>
                                <el-form-item label="部门" prop="deptId">
                                    <el-tree-select v-model="queryParams.deptId" :data="deptOptions"
                                        :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
                                        placeholder="请选择部门" check-strictly />
                                </el-form-item>
                                <el-form-item label="状态" prop="status">
                                    <el-select v-model="queryParams.status" placeholder="岗位状态" clearable>
                                        <el-option v-for="dict in sys_normal_disable" :key="dict.value"
                                            :label="dict.label" :value="dict.value" />
                                    </el-select>
                                </el-form-item>
                                <el-form-item>
                                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                                </el-form-item>
                            </el-form>
                        </el-card>
                    </div>
                </transition>
                <el-card shadow="hover">
                    <template #header>
                        <el-row :gutter="10" class="mb8">
                            <el-col :span="1.5">
                                <el-button v-hasPermi="['system:post:add']" type="primary" plain icon="Plus"
                                    @click="handleAdd">新增</el-button>
                            </el-col>
                            <el-col :span="1.5">
                                <el-button v-hasPermi="['system:post:edit']" type="success" plain icon="Edit"
                                    :disabled="single" @click="handleUpdate">修改</el-button>
                            </el-col>
                            <el-col :span="1.5">
                                <el-button v-hasPermi="['system:post:remove']" type="danger" plain icon="Delete"
                                    :disabled="multiple" @click="handleDelete">删除</el-button>
                            </el-col>
                            <el-col :span="1.5">
                                <el-button v-hasPermi="['system:post:export']" type="warning" plain icon="Download"
                                    @click="handleExport">导出</el-button>
                            </el-col>
                            <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
                        </el-row>
                    </template>
                    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
                        <el-table-column type="selection" width="55" align="center" />
                        <el-table-column label="岗位编码" align="center" prop="postCode" />
                        <el-table-column label="类别编码" align="center" prop="postCategory" />
                        <el-table-column label="岗位名称" align="center" prop="postName" />
                        <el-table-column label="部门" align="center" prop="deptName" />
                        <el-table-column label="状态" align="center" prop="status">
                            <template slot-scope="scope">
                                <dict-tag :options="dict.sys_normal_disable" :value="scope.row.status" />
                            </template>
                        </el-table-column>
                        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                            <template #default="scope">
                                <span>{{ parseTime(scope.row.createTime) }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="180" align="center">
                            <template #default="scope">
                                <el-tooltip content="修改" placement="top">
                                    <el-button v-hasPermi="['system:post:edit']" link type="text" icon="Edit"
                                        @click="handleUpdate(scope.row)">操作</el-button>
                                </el-tooltip>
                                <el-tooltip content="删除" placement="top">
                                    <el-button v-hasPermi="['system:post:remove']" link type="text" icon="Delete"
                                        @click="handleDelete(scope.row)">删除</el-button>
                                </el-tooltip>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination v-show="total > 0" v-model:page="queryParams.pageNum"
                        v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
                </el-card>
                <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" append-to-body>
                    <el-form ref="postFormRef" :model="form" :rules="rules" label-width="80px">
                        <el-form-item label="岗位名称" prop="postName">
                            <el-input v-model="form.postName" placeholder="请输入岗位名称" />
                        </el-form-item>
                        <el-form-item label="部门" prop="deptId">
                            <el-tree-select v-model="form.deptId" :data="deptOptions"
                                :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"
                                placeholder="请选择部门" check-strictly />
                        </el-form-item>
                        <el-form-item label="岗位编码" prop="postCode">
                            <el-input v-model="form.postCode" placeholder="请输入编码名称" />
                        </el-form-item>
                        <el-form-item label="类别编码" prop="postCategory">
                            <el-input v-model="form.postCategory" placeholder="请输入类别编码" />
                        </el-form-item>
                        <el-form-item label="岗位顺序" prop="postSort">
                            <el-input-number v-model="form.postSort" controls-position="right" :min="0" />
                        </el-form-item>
                        <el-form-item label="岗位状态" prop="status">
                            <el-radio-group v-model="form.status">
                                <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :value="dict.value">{{
                                    dict.label
                                    }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                    </el-form>
                    <template #footer>
                        <div class="dialog-footer">
                            <el-button type="primary" @click="submitForm">确 定</el-button>
                            <el-button @click="cancel">取 消</el-button>
                        </div>
                    </template>
                </el-dialog>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import { listPost, addPost, delPost, getPost, updatePost } from '@/api/system/post';
import { deptTreeSelect } from "@/api/system/user";

export default {
    data() {
        return {
            deptName: '',
            deptOptions: [],
            postList: [],
            showSearch: true,
            loading: true,
            single: true,
            multiple: true,
            total: 0,
            ids: [],
            dialog: {
                title: '',
                visible: false
            },
            queryParams: {
                postCode: '',
                postCategory: '',
                postName: '',
                deptId: '',
                status: '',
                pageNum: 1,
                pageSize: 10
            },
            form: {
                postCode: '',
                postCategory: '',
                postName: '',
                deptId: '',
                postSort: '',
                status: '',
                remark: ''
            },
            rules: {
                postCode: [{ required: true, message: '请输入岗位编码', trigger: 'blur' }],
                postCategory: [{ required: true, message: '请输入岗位类别编码', trigger: 'blur' }],
                postName: [{ required: true, message: '请输入岗位名称', trigger: 'blur' }],
                deptId: [{ required: true, message: '请选择部门', trigger: 'change' }],
                postSort: [{ required: true, message: '请输入岗位顺序', trigger: 'change' }]
            },
            sys_normal_disable: [],
            animate: {
                searchAnimate: {
                    enter: 'fade-in',
                    leave: 'fade-out'
                }
            }
        };
    },
    methods: {
        handleQuery() {
            this.getList();
        },
        resetQuery() {
            this.queryParams = {
                postCode: '',
                postCategory: '',
                postName: '',
                deptId: '',
                status: '',
                pageNum: 1,
                pageSize: 10
            };
            this.getList();
        },
        handleAdd() {
            this.dialog.visible = true;
            this.dialog.title = '新增岗位';
            this.form = {
                postCode: '',
                postCategory: '',
                postName: '',
                deptId: '',
                postSort: '',
                status: '',
                remark: ''
            };
        },
        handleUpdate(row) {
            this.dialog.visible = true;
            this.dialog.title = '修改岗位';
            this.getPost(row.postId);
        },
        handleDelete(row) {
            this.$confirm('是否确认删除岗位?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                return delPost(row.postId).then(() => {
                    this.$message.success('删除成功');
                    this.getList();
                });
            });
        },
        handleExport() {
            // 导出功能
        },
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.postId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        getList() {
            this.loading = true;
            listPost(this.queryParams).then(response => {
                this.postList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        getPost(postId) {
            getPost(postId).then(response => {
                this.form = response.data;
            });
        },
        submitForm() {
            this.$refs['postFormRef'].validate(valid => {
                if (valid) {
                    const api = this.dialog.title === '新增岗位' ? addPost : updatePost;
                    api(this.form).then(() => {
                        this.$message.success('操作成功');
                        this.dialog.visible = false;
                        this.getList();
                    });
                }
            });
        },
        cancel() {
            this.dialog.visible = false;
        },
        filterNode(value, data) {
            if (!value) return true;
            return data.label.includes(value);
        },
        parseTime(time) {
            // 格式化时间函数
        },
        /** 节点单击事件 */
        handleNodeClick(data) {
            queryParams.value.belongDeptId = data.id;
            queryParams.value.deptId = undefined;
            this.handleQuery();
        },
        getTreeSelect() {
            deptTreeSelect().then(response => {
                this.deptOptions = response.data
            });
        }
    },
    watch: {
        deptName(val) {
            this.$refs.deptTreeRef.filter(val);
        }
    },
    created() {
        this.getList();
        this.getTreeSelect();
        this.sys_normal_disable = [
            { label: '正常', value: '0' },
            { label: '禁用', value: '1' }
        ];
    }
};
</script>

<style scoped>
.mt-2 {
    margin-top: 20px;
}
</style>
